package cn.edu.scau.cmi.tangqingning.controll;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scau.cmi.tangqingning.dao.UserDao;
import cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl;
import cn.edu.scau.cmi.tangqingning.model.Group;

/**
 * 控制层-添加项目信息的逻辑处理<br>
 * 当客户使用get方式请求Servlet时，Web容器调用doGet方法处理请求，通过调用UserDao的addProData方法添加小组
 * @author 周华智
 *@see cn.edu.scau.cmi.tangqingning.dao.UserDao#addProData
 */
public class AddProDataServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * 当客户使用get方式请求Servlet时，Web容器调用doGet方法处理请求
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 重写doGet方法
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int groupNumber = Integer.parseInt(request.getParameter("groupNumber"));
		String proIntroduction = request.getParameter("proIntroduction");
		String proRequirement = request.getParameter("proRequirement");
		String proDivision = request.getParameter("proDivision");
		Group group = new Group(groupNumber, proIntroduction, proRequirement, proDivision);
		UserDao userdao = new UserDaoImpl();

		boolean flag = userdao.addProData(group);
		if (flag) {
			request.setAttribute("msg", "<------添加小组成功------>");
			request.getRequestDispatcher("userMain.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "<------添加小组失败------>");
			request.getRequestDispatcher("userMain.jsp").forward(request, response);
		}

	}

}
